const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Start_Date: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        End_Date: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
        Supplier_SerialNumber: [{ required: true, message: '请输入供应商流水号', trigger: 'blur' }],
      },
      supplier_info: [],
      article_info: [],
      isDisabled: false,
      form: {     //采购价格单
        Purchase_Unit_Price_SerialNumber: "",
        Start_Date: '',
        End_Date: '',
        Supplier_SerialNumber: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          purchase_unitprice_detail: [{     //采购价格单明细
            Purchase_Unit_Price_Detail_SerialNumber: "",
            Purchase_Unit_Price_SerialNumber: '',
            Article_SerialNumber: '',
            Unit_Price: '',
            Purchase_Unit_Price: '',
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      let _this = this;
      $.ajax({
        url: DomainName + '/GoodsFileInquiry',
        type: 'POST',
        data: { Data: "supplier_info" },
        success: function (res) {
          _this.supplier_info = res
        }
      })
      $.ajax({
        url: DomainName + '/ArticleInfo',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.article_info = res
        }
      })
    },
    submitForm(formName) {
      let _this = this
      if (_this.form.Children.purchase_unitprice_detail[0].Article_SerialNumber == '' || _this.form.Children.purchase_unitprice_detail[0].Purchase_Unit_Price == '' || _this.form.Children.purchase_unitprice_detail[0].Unit_Price == '') {
        return _this.$message.error("请完善信息")
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: DomainName + '/GoodsFileAdd',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                purchase_unitprice: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './purchaseUnitprice.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    Change_Unit_Price(index) {
      this.isDisabled = true
      let str = this.form.Children.purchase_unitprice_detail[index].Article_SerialNumber
      for (let i = 0; i < this.article_info.length; i++) {
        if (this.article_info[i].Article_SerialNumber == str) {
          this.form.Children.purchase_unitprice_detail[index].Unit_Price = this.article_info[i].Unit_Price
        }
      }
    },
    add_purchase_unitprice_detail() {
      this.form.Children.purchase_unitprice_detail.push({     //采购价格单
        Purchase_Unit_Price_Detail_SerialNumber: "",
        Purchase_Unit_Price_SerialNumber: '',
        Article_SerialNumber: '',
        Unit_Price: '',
        Purchase_Unit_Price: '',
      })
    },
    remove_purchase_unitprice_detail() {
      if (this.form.Children.purchase_unitprice_detail.length > 1) {
        this.form.Children.purchase_unitprice_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})